/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Visor.java
 *
 * Created on 19-ago-2011, 14:34:01
 */
package sink;

import datasource.ImageReader;
import datasource.ImageWriter;
import datasource.Imagen;
import filter.Filter;
import filter.IdentityFilter;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;

/**
 *
 * @author Fabian
 */
public class Visor extends javax.swing.JFrame {

    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    Imagen imagen;
    Lienzo lienzo;

    public void customInitComponents() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        this.setSize(640, 480);

        jMenu1.setText("Archivo");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Abrir imagen");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Generar");

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Generar imagen monocromatica");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);
        jMenuBar1.add(jMenu2);
        setJMenuBar(jMenuBar1);


    }

    /** Creates new form Visor */
    public Visor() {
        //initComponents();
        customInitComponents();
        /*
        long entero=255;
        
        byte[] a=BitWise.longToByteArrayLE(entero);
        for (int i = 0; i < a.length; i++) {
        System.out.println("intToByteArray" + i + " value" + a[i]);
        }
        System.out.println("byteArrayToIntLE " +BitWise.byteArrayToLongLE(a));
         */
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 420, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 310, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
        JFileChooser fc = new JFileChooser();
        int returnVal = fc.showSaveDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            //System.out.print(file.getPath());
            ImageWriter.createRandomMonoImage(200, 200, file.getPath());
        } else {
        }
        System.out.println("generar imagen");
    }

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
        JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(this);
        ImageReader ireader;
        imagen = null;
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                File file = fc.getSelectedFile();
                ireader = new ImageReader(file.getPath());

                imagen = ireader.getImagenCopy();

                Filter filter = new IdentityFilter();
                configureLienzo();
                lienzo.setImage(imagen);
                
                lienzo.repaint();

                System.out.println("abrir imagen");

            } catch (IOException ex) {
                Logger.getLogger(Visor.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
        }
    }

    private void configureLienzo() {
        lienzo = new Lienzo();

        lienzo.addMouseListener(new java.awt.event.MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                canvas_mousePressed(e);
            }

            private void canvas_mousePressed(MouseEvent e) {
                int i;
                Point p = e.getPoint();
            }
        });
        this.add(lienzo);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(lienzo, javax.swing.GroupLayout.DEFAULT_SIZE, 640, Short.MAX_VALUE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(lienzo, javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE));

        pack();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Visor().setVisible(true);
                //lienzo.repaint();
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
